package com.bootdo.app.controller;

import com.bootdo.app.htmlVO.car.Car;
import com.bootdo.app.htmlVO.car.CarGoods;
import com.bootdo.app.htmlVO.car.CarInfo;
import com.bootdo.app.htmlVO.customer.UserOrderListVO;
import com.bootdo.app.service.AppOrderProductService;
import com.bootdo.app.service.AppOrderService;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.CarUtils;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.domain.*;
import com.bootdo.appsystem.service.*;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.redis.shiro.RedisCache;
import com.bootdo.common.redis.shiro.RedisManager;

import com.bootdo.common.utils.HttpContextUtils;
import com.bootdo.common.utils.JSONUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 【@author 】 @fan
 * 【@Email  】 mooncluyed@outlook.com
 * 【类解释  】
 * 【@Date   】 2018/5/17 0017
 **/
@Controller
@RequestMapping("/app/order/")
public class AppOrderController extends BaseController{

    @Autowired
    private AppOrderService appOrderService;
    @Autowired
    private OrderproductService orderproductService;
    @Autowired
    private AppOrderProductService appOrderProductService;
    @Autowired
    private ProducesinfoService producesinfoService;
    @Autowired
    private CarService carService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private OrderproductitemsService orderproductitemsService;
    @Autowired
    private MessageService messageService;


    @ResponseBody
    @GetMapping("creat")
    public ServerResponse getCookies(String ordercode){
//         return appOrderService.creatOrderByWeChat(1,800.0,1);
        return appOrderService.chackOrderStatu(ordercode,3);
//        return appOrderService.fafangHongbao();
    }
    @ResponseBody
    @GetMapping("getorder")
    public ServerResponse getorder(){
        for (int i = 0 ; i < 10000 ; i++){
            appOrderService.creatOrderByWeChat(1,800.0,1);
        }
        //m
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("ostatus" , -1);
        List<OrderproductDO> orderproductDOList = orderproductService.list(map);
        for (OrderproductDO orderproductDO : orderproductDOList) {
            appOrderService.shengChengOrderStatu(orderproductDO.getOid(),5);
        }
        return ServerResponse.createBySuccess();
        //appOrderService.chackOrderStatu(ordercode,3);
    }


    //发货请求
    @ResponseBody
    @PostMapping("msgFahuo")
    public ServerResponse msgOneFahuo(Integer oitem , String adsCode){
        OrderproductitemsDO orderproductitemsDO = new OrderproductitemsDO();
        orderproductitemsDO.setOitemid(oitem);
        orderproductitemsDO.setGoodsstatus(2);
        int update = orderproductitemsService.update(orderproductitemsDO);
        if(update > 0){
            OrderproductitemsDO orderitem= orderproductitemsService.get(oitem);
            OrderproductDO orderproductDO = orderproductService.get(orderitem.getOid());
            //给会员发消息
            MessageDO messageDO = new MessageDO();
            messageDO.setTitle("您的订单已经发货了");
            messageDO.setUid(orderproductDO.getUid());
            messageDO.setAddtime(new Date());
            messageDO.setCustomername("系统消息，订单发货");
            messageDO.setIsread(0);
            messageDO.setContent("<p>尊敬的用户您好：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " +
                    "您的订单：<a location.href = '/app/seller/toUserOrderDetail?orderCode=" +
                    orderproductDO.getOrdercode()
                    +"'target=\"_blank\">" +
                    orderproductDO.getOrdercode()
                    +"</a>已经发货<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " +
                    "快递单号为：<a target=\"_blank\">"+
                    adsCode
                    +"</a><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 请您保持通讯畅通。<br>   <span>感谢您的支持！</span> </p>");
            messageDO.setIntro("您的订单已经发货，请查看订单详情");
            messageDO.setIstop(1);
            messageService.save(messageDO);

            //todo 给会员发送微信订单发货消息


            orderproductDO.setOstatus(2);
            orderproductService.update(orderproductDO);

            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("发货失败");
    }


    //用户确认收货请求
    @ResponseBody
    @PostMapping("goodsrusuns")
    public ServerResponse goodsrusuns(Integer oitem){
        OrderproductitemsDO orderproductitemsDO = new OrderproductitemsDO();
        orderproductitemsDO.setOitemid(oitem);
        orderproductitemsDO.setGoodsstatus(3);
        int update = orderproductitemsService.update(orderproductitemsDO);

        return ServerResponse.createBySuccess("修改成功");
    }










    //发货请求
    @ResponseBody
    @PostMapping("msgCodeFahuo")
    public ServerResponse msgCodeFahuo(String code){
        List<OrderproductDO> byOrderCode = orderproductService.getByOrderCode(code);
        for (OrderproductDO orderproductDO : byOrderCode) {

        }
        return ServerResponse.createByErrorMessage("发货失败");
    }



















    //创建一个新的商品订单
    @ResponseBody
    @PostMapping("creatNewGoodsOrder")
    public ServerResponse creatNewGoodsOrder(String carJson , String addressJson , Double totalMoney , Double totalFare){

        //购买商品信息转换成类
        Car car = new Car();
        car = (Car)JSONUtils.jsonToBean(carJson,car);

        //用户地址信息转换成类
        ShipaddressDO address = new ShipaddressDO();
        address = (ShipaddressDO)JSONUtils.jsonToBean(addressJson,address);

        //创建一个新的商品订单
        appOrderService.creatNewGoodsOrder(car,address,totalMoney,totalFare);

        //将购物车已勾选数据删除
        CarDO byUid = carService.getByUid(address.getUid());
        car = (Car)JSONUtils.jsonToBean(byUid.getCarjson(),car);
        car = CarUtils.isChick(car,1);
        carJson = JSONUtils.beanToJson(car);

        if(byUid != null){
            byUid.setCarjson(carJson);
            carService.update(byUid);
        }

        return ServerResponse.createBySuccess();
    }


    /**
     * 购物车支付商品订单页面
     * @param model
     * @return
     */
    @GetMapping("toPayGood")
    public String toPayGood(Model model){
        UseraccountDO useraccountDO = (UseraccountDO)HttpContextUtils.getHttpServletRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        CarDO carinfo = carService.getByUid(useraccountDO.getUid());

        Car car = new Car();
        car = (Car)JSONUtils.jsonToBean(carinfo.getCarjson(),car);
        car = CarUtils.isChick(car,0);

        model.addAttribute("carinfo",JSONUtils.beanToJson(car));
        return "app/order/orderTo";
    }

    /**
     * 商品直接点击购买
     * @param goodsid
     * @return
     */
    @GetMapping("toPayGoodsOnly")
    public String toPayGoodsOnly(Integer goodsid , Model model){
        //商品订单
        ProducesinfoDO producesinfoDO = producesinfoService.get(goodsid);

        //商家信息
        SellerDO sellerDO = sellerService.get(producesinfoDO.getSellerid());

        Car car = new Car();

        CarInfo carInfo = new CarInfo();
        carInfo.setChecked(1);
        carInfo.setSellerName(sellerDO.getSellername());
        carInfo.setSellerImage(sellerDO.getSellerimage());
        carInfo.setSellerId(sellerDO.getSellerid());

        ArrayList<CarGoods> carGoodList = new ArrayList<>();
        CarGoods carGoods = new CarGoods();
        carGoods.setNumber(1);
        carGoods.setImgUrl(producesinfoDO.getFavorable());
        carGoods.setIsChick(1);
        carGoods.setPrice(producesinfoDO.getShopprice());
        carGoods.setVolume(producesinfoDO.getVolume());
        carGoods.setGoodsTitle(producesinfoDO.getPname());
        carGoods.setPid(producesinfoDO.getPid());
        carGoodList.add(carGoods);

        carInfo.setGoodList(carGoodList);

        ArrayList<CarInfo> carInfos = new ArrayList<>();
        carInfos.add(carInfo);

        car.setCarInfo(carInfos);

        model.addAttribute("carinfo",JSONUtils.beanToJson(car));
        return "app/order/orderTo";
    }




    @GetMapping("get")
    @ResponseBody
    public Object getSession(String JSessionID){
        RedisCache<String , Object> redisCache = new RedisCache<String , Object>(new RedisManager());
        return redisCache.get(JSessionID);
    }

    //去用户订单列表
    @GetMapping("toUserOrder")
    public String toUserOrder(Integer i, Model model){
        model.addAttribute("i" , i);
        return "app/customer/myorder";
    }

    /**
     * 用户订单
     * @param map
     * @return
     */
    @PostMapping("msgUserOrder")
    @ResponseBody
    public ServerResponse<PageUtils> msgUserOrder(@RequestParam Map<String , Object> map) {
        Query query = new Query(map);
        UseraccountDO sessionCustomer = (UseraccountDO)this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        query.put("uid" , sessionCustomer.getUid());
        List<UserOrderListVO> userOrderListVOSList= appOrderProductService.userOrderlist(query);
        int total = appOrderProductService.userOrdercount(query);
        PageUtils pageUtils = new PageUtils(userOrderListVOSList , total);
        return ServerResponse.createBySuccess(pageUtils);
    }





}
